import {api,host} from "./apiList.js";

function post(url, params) {
  return new Promise((resolve, reject) => {
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wx.request({
      url: host + api.get(url),
      timeout: 10000,
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      method: "POST",
      data: params,
      success: data => {
        wx.hideLoading({
          success: (res) => {

          },
        })
        if (data.statusCode == "200") {
          if(data.data.errCode == "000") {
            resolve(data.data.data);
          }else {
            wx.showToast({
              title: data.data.errMsg,
              icon: "none",
              duration: 2000
            })
            reject(data.data)
          }
        }else {
          wx.showToast({
            title: data.data.message,
            icon: "none",
            duration: 2000
          })
          reject(data.data)
        }
      },
      fail: error => {
        reject(error);
      },
      complete: function() {
        console.log("进行了请求")
      }
    })
  });
}

function restPost(url, params) {
  return new Promise((resolve, reject) => {
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wx.request({
      url: host + api.get(url),
      timeout: 10000,
      header: {
        "content-type": "application/json"
      },
      method: "POST",
      data: params,
      success: data => {
        wx.hideLoading({
          success: (res) => {

          },
        })
        if (data.statusCode == "200") {
          if(data.data.errCode == "000") {
            resolve(data.data.data);
          }else {
            wx.showToast({
              title: data.data.errMsg,
              icon: "none",
              duration: 2000
            })
            reject(data.data)
          }
        }else {
          wx.showToast({
            title: data.data.message,
            icon: "none",
            duration: 2000
          })
          reject(data.data)
        }
      },
      fail: error => {
        reject(error);
      },
      complete: function() {
        console.log("进行了请求")
      }
    })
  });
}
module.exports = {
  post,
  restPost
}